{
 "metadata": {
  "name": "07-SimpleEODs"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Elementos \u00d3pticos Difractivos"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from pyoptools.all import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Polinomios en pyOpTools"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "p2d=poly2d([0,0,0,1,0,1])\n",
      "\n",
      "X,Y=meshgrid(linspace(-5,5,20),linspace(-5,5,20))\n",
      "\n",
      "Z=p2d.eval(X,Y)\n",
      "imshow(Z,extent=(-5,5,-5,5));colorbar()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Ejemplo 1: Rejilla de difracci\u00f3n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "g=RPPMask(shape=Rectangular(size=(10,10)), phm=poly2d([0,2*pi*600,0,]),M=[1])#,0,-1,-2])\n",
      "\n",
      "oc=Component(surflist=[(g, (0, 0, 0), (0, 0, 0)), \n",
      "                       ])\n",
      "ccd=CCD(size=(1000,50))\n",
      "\n",
      "\n",
      "\n",
      "os=System(complist=[(oc,(0,0,20),(0,0,0)),\n",
      "                    (ccd,(0,0,120),(0,0,0)),\n",
      "                    ],n=1)\n",
      "r=[]\n",
      "#Test using many wavelenghts\n",
      "for w in (.4,.45,.5,.53,.6,.65,.7):\n",
      "    r.append(Ray(wavelength=w))\n",
      "\n",
      "#Add the ray sources\n",
      "os.ray_add(r)\n",
      "\n",
      "\n",
      "os.propagate()\n",
      "\n",
      "display(plot3D(os,center=(0,0,60), size=(150,150),scale=2,rot=[(0,0,pi/2),(0,pi/2+0.1,0)]))\n",
      "spot_diagram_c(ccd)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "n=1.\n",
      "d=1/600.\n",
      "\n",
      "for l in (.4e-3,.45e-3,.5e-3,.53e-3,.6e-3,.65e-3,.7e-3):\n",
      "    th=arcsin(n*l/d)\n",
      "    print \"lambda = {:6.2E}  Intersecci\u00f3n = {:4.3E}\".format(l,100*tan(th))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Ejemplo 2: Corrigiendo aberraciones con un EOD"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Sistema aberrado"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "L2=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "L3=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "\n",
      "C=CCD()\n",
      "\n",
      "HOLO=CCD(size=(50,50))\n",
      "\n",
      "S=System(complist=[(L2,(0,0,100),(0,0,0)),(HOLO,(0,0,110),(0,0,0)),(L3,(0,0,120),(0,0,0)),(C,(0,0,215),(0,0,0)) ],n=1)\n",
      "\n",
      "R=point_source_p(origin=(0.,0.,0.),direction=(0.,0.,0),span=pi/14\n",
      "                   ,num_rays=(50,50),wavelength=0.470, label=\"blue\")\n",
      "\n",
      "S.ray_add(R)\n",
      "S.propagate()\n",
      "plot3D(S,center=(0,0,100),size=(250,100),scale=4,rot=[(0,pi/2,0),(pi/20,-pi/10,0)])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "spot_diagram(C)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "imshow(HOLO.get_optical_path_map())\n",
      "colorbar()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pol1,err1=HOLO.get_optical_path_map_lsq(order=15)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Propagaci\u00f3n de para atras"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "L2=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "L3=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "\n",
      "C=CCD()\n",
      "\n",
      "HOLO=CCD(size=(50,50))\n",
      "\n",
      "S=System(complist=[(L2,(0,0,100),(0,0,0)),(HOLO,(0,0,110),(0,0,0)),(L3,(0,0,120),(0,0,0)),(C,(0,0,215),(0,0,0)) ],n=1)\n",
      "\n",
      "R=point_source_p(origin=(0.,0.,215.),direction=(0.,pi,0.),span=pi/14\n",
      "                   ,num_rays=(50,50),wavelength=0.470, label=\"blue\")\n",
      "\n",
      "S.ray_add(R)\n",
      "S.propagate()\n",
      "plot3D(S,center=(0,0,100),size=(250,100),scale=4,rot=[(0,pi/2,0),(pi/20,-pi/10,0)])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "imshow(HOLO.get_optical_path_map())\n",
      "colorbar()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pol2,err2=HOLO.get_optical_path_map_lsq(order=15)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Sistema corregido"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "L2=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "L3=SphericalLens(radius=25,curvature_s1=1./100.,curvature_s2=-1./100,thickness=10,material=schott[\"BK7\"])\n",
      "\n",
      "C=CCD()\n",
      "\n",
      "HOLO=RPPMask(shape=Rectangular(size=(50,50)), phm=(2*pi/.470e-3)*(-pol1-pol2),M=[1])#,0,-1,-2])\\\n",
      "oc=Component(surflist=[(HOLO, (0, 0, 0), (0, 0, 0)), \n",
      "                       ])\n",
      "\n",
      "S=System(complist=[(L2,(0,0,100),(0,0,0)),(oc,(0,0,110),(0,0,0)),(L3,(0,0,120),(0,0,0)),(C,(0,0,215),(0,0,0)) ],n=1)\n",
      "\n",
      "R=point_source_p(origin=(0.,0.,0.),direction=(0.,0.,0),span=pi/18\n",
      "                   ,num_rays=(10,10),wavelength=0.470, label=\"blue\")\n",
      "\n",
      "#R1=point_source_p(origin=(0.,0.,0.),direction=(0.,0.,0),span=pi/18\n",
      "#                   ,num_rays=(10,10),wavelength=0.530)\n",
      "S.ray_add(R)\n",
      "#S.ray_add(R1)\n",
      "\n",
      "S.propagate()\n",
      "plot3D(S,center=(0,0,100),size=(250,100),scale=4,rot=[(0,pi/2,0),(pi/20,-pi/10,0)])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "spot_diagram_c(C)\n",
      "title(\"Diagrama de Manchas\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Holograma"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "holo=(2*pi/.470e-3)*(-pol1-pol2)\n",
      "\n",
      "X,Y=meshgrid(linspace(-25,25,1000),linspace(-25,25,1000))\n",
      "\n",
      "figsize(15,15)\n",
      "imshow(holo.meval(X,Y)%(2*pi),extent=(-25,25,-25,25),cmap=\"gray\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}